package com.example.utils;

import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.bg.CircleBackground;
import com.kennycason.kumo.bg.PixelBoundryBackground;
import com.kennycason.kumo.font.KumoFont;
import com.kennycason.kumo.font.scale.LinearFontScalar;
import com.kennycason.kumo.font.scale.SqrtFontScalar;
import com.kennycason.kumo.nlp.FrequencyAnalyzer;
import com.kennycason.kumo.palette.ColorPalette;
import com.kennycason.kumo.palette.LinearGradientColorPalette;

import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**词云图工具类
 *
 */
public class WordCloudUtils {
    public static void wordCloud(List<String> words,String path){
        final FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
        frequencyAnalyzer.setWordFrequenciesToReturn(250);
        frequencyAnalyzer.setMinWordLength(2);
        //frequencyAnalyzer.setWordTokenizer(new ChineseWordTokenizer());
        // 可以直接从文件中读取
        //final List<WordFrequency> wordFrequencies = frequencyAnalyzer.load(getInputStream("text/chinese_language.txt"));
        final List<WordFrequency> wordFrequencies = new ArrayList<WordFrequency>();
        //加入分词并随机生成权重，每次生成得图片都不一样
        for (String word : words) {
            wordFrequencies.add(new WordFrequency(word,new Random().nextInt(word.length())));
        }
        //此处不设置会出现中文乱码
        java.awt.Font font = new java.awt.Font("STSong-Light", 2, 18);
        final Dimension dimension = new Dimension(400, 400);
        final WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
        wordCloud.setPadding(2);
        wordCloud.setBackgroundColor(new Color(252,255, 242));
        wordCloud.setBackground(new CircleBackground(255));
        wordCloud.setFontScalar(new SqrtFontScalar(12, 42));
        //设置词云显示的三种颜色，越靠前设置表示词频越高的词语的颜色
        wordCloud.setColorPalette(new LinearGradientColorPalette(Color.RED, Color.BLUE, Color.GREEN, 30, 30));
        wordCloud.setKumoFont(new KumoFont(font));
        //因为我这边是生成一个圆形,这边设置圆的半径
        wordCloud.setBackground(new CircleBackground(200));
        wordCloud.build(wordFrequencies);
        wordCloud.writeToFile(path);
    }

    public static void wordCloud2(List<String> words,String path){
        final FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
        frequencyAnalyzer.setWordFrequenciesToReturn(500);
        frequencyAnalyzer.setMinWordLength(4);
        final List<WordFrequency> wordFrequencies = new ArrayList<WordFrequency>();
        //加入分词并随机生成权重，每次生成得图片都不一样
        for (String word : words) {
            wordFrequencies.add(new WordFrequency(word,new Random().nextInt(word.length())));
        }
        //此处不设置会出现中文乱码
        java.awt.Font font = new java.awt.Font("STSong-Light", 2, 18);
        final Dimension dimension = new Dimension(400, 400);
        final WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
        wordCloud.setPadding(2);
        wordCloud.setBackgroundColor(new Color(252,255, 242));
        wordCloud.setBackground(new CircleBackground(200));
        wordCloud.setKumoFont(new KumoFont(font));
        // colors followed by and steps between
        wordCloud.setColorPalette(new LinearGradientColorPalette(Color.RED, Color.BLUE, Color.GREEN, 30, 30));
        wordCloud.setFontScalar( new SqrtFontScalar(10, 40));
        wordCloud.build(wordFrequencies);
        wordCloud.writeToFile(path);
    }

    public static void wordCloud3(List<String> words,String path){
        final FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
        frequencyAnalyzer.setWordFrequenciesToReturn(500);
        frequencyAnalyzer.setMinWordLength(4);
        //此处不设置会出现中文乱码
        java.awt.Font font = new java.awt.Font("STSong-Light", 2, 18);
        final List<WordFrequency> wordFrequencies = new ArrayList<WordFrequency>();
        //加入分词并随机生成权重，每次生成得图片都不一样
        for (String word : words) {
            wordFrequencies.add(new WordFrequency(word,new Random().nextInt(word.length())));
        }
        final Dimension dimension = new Dimension(400, 400);
        final WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
        wordCloud.setPadding(2);
        wordCloud.setBackgroundColor(new Color(252,255, 242));
        wordCloud.setBackground(new CircleBackground(200));
        wordCloud.setKumoFont(new KumoFont(font));
// colors followed by and steps between
        wordCloud.setColorPalette(new LinearGradientColorPalette(Color.RED, Color.BLUE, Color.GREEN, 30, 30));
        wordCloud.setFontScalar( new SqrtFontScalar(10, 40));
        wordCloud.build(wordFrequencies);
        wordCloud.writeToFile(path);
    }

    public static void wordCloud4(List<String> words,String path){
        final FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
        frequencyAnalyzer.setWordFrequenciesToReturn(300);
        frequencyAnalyzer.setMinWordLength(4);
        java.awt.Font font = new java.awt.Font("STSong-Light", 2, 18);
        final List<WordFrequency> wordFrequencies = new ArrayList<WordFrequency>();
        //加入分词并随机生成权重，每次生成得图片都不一样
        for (String word : words) {
            wordFrequencies.add(new WordFrequency(word,new Random().nextInt(word.length())));
        }

        final Dimension dimension = new Dimension(500, 312);
        final WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
        wordCloud.setPadding(2);
        try {
            wordCloud.setBackground(new PixelBoundryBackground("D:/bk.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        wordCloud.setBackgroundColor(new Color(252,255, 242));
        wordCloud.setKumoFont(new KumoFont(font));
        wordCloud.setColorPalette(new ColorPalette(new Color(0x4055F1), new Color(0x408DF1), new Color(0x40AAF1), new Color(0x40C5F1), new Color(0x40D3F1), new Color(0xFFFFFF)));
        wordCloud.setFontScalar(new LinearFontScalar(10, 40));
        wordCloud.build(wordFrequencies);
        wordCloud.writeToFile(path);
    }
}
